package org.springframework.boot;

import org.apache.commons.logging.impl.NoOpLog;
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
import org.springframework.boot.context.event.EventPublishingRunListener;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.env.EnvironmentPostProcessorApplicationListener;
import org.springframework.boot.env.RandomValuePropertySourceEnvironmentPostProcessor;
import org.springframework.boot.logging.DeferredLogs;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.ResourcePropertySource;

public class Step5 {
    public static void main1(String[] args) throws Exception{
        SpringApplication springApplication=new SpringApplication();
        ApplicationEnvironment env = new ApplicationEnvironment();
        System.out.println("调用前----");
        for (PropertySource<?> ps : env.getPropertySources()) {
            System.out.println(ps);
        };
        ConfigDataEnvironmentPostProcessor environmentPostProcessor=new ConfigDataEnvironmentPostProcessor(new DeferredLogs(),new DefaultBootstrapContext());
        System.out.println("environmentPostProcessor.postProcessEnvironment(env,springApplication)调用后----");
        for (PropertySource<?> ps : env.getPropertySources()) {
            System.out.println(ps);
        };
        environmentPostProcessor.postProcessEnvironment(env,springApplication);
        RandomValuePropertySourceEnvironmentPostProcessor postProcessor2=new RandomValuePropertySourceEnvironmentPostProcessor(new NoOpLog());
        postProcessor2.postProcessEnvironment(env,springApplication);
        System.out.println("RandomValuePropertySourceEnvironmentPostProcessor调用后----");
        for (PropertySource<?> ps : env.getPropertySources()) {
            System.out.println(ps);
        };
        System.out.println(env.getProperty("random.long"));
        System.out.println(env.getProperty("random.long"));
        System.out.println(env.getProperty("random.uuid"));
    }

    public static void main(String[] args) {
        SpringApplication springApplication=new SpringApplication();
        ApplicationEnvironment env = new ApplicationEnvironment();
        System.out.println("publisher.environmentPrepared发布前---");
        springApplication.addListeners(new EnvironmentPostProcessorApplicationListener());
        for (PropertySource<?> ps : env.getPropertySources()) {
            System.out.println(ps);
        };
        //第五步发布事件
        EventPublishingRunListener publisher = new EventPublishingRunListener(springApplication,args);
        publisher.environmentPrepared(new DefaultBootstrapContext(),env);
        System.out.println("publisher.environmentPrepared发布后---");
        for (PropertySource<?> ps : env.getPropertySources()) {
            System.out.println(ps);
        };
    }
}
